#include "activeevent.h"
#include <unistd.h>
#include <string.h>
#include "log.h"
#include "Game.h"

CActiveEvent::CActiveEvent() : CPollerObject(), m_pServer(NULL)
{

}
CActiveEvent::~CActiveEvent()
{

}

int CActiveEvent::Init()
{
	CPollerObject::EnableInput();

	return 0;
}

int CActiveEvent::create(void)
{
	netfd = eventfd(0, 0);
	if (netfd == -1){
		log_error("netfd == -1");
        if(netfd >= 0){
            close(netfd);
            netfd = -1;
        }
        return -1;
	}
	return 0;
}

int CActiveEvent::signal(void)
{
    int64 value = 1;
    int nwrite = write(netfd, &value, sizeof(int64));
    if(nwrite != sizeof(int64)){
        log_error("write failed netfd=%d", netfd);
        return -1;
    }
    return 0;
}
int CActiveEvent::InputNotify(void)
{
    int64 value = 0;
    read(netfd, &value, sizeof(int64));
    // 执行事件处理
    m_pServer->OnActiveEvent();
	return 0;
}

int CActiveEvent::HangupNotify(void)
{
	log_error("hangup,fd,%d", netfd);

	return 0;
}